package com.xuecheng.content.controller;


import com.xuecheng.api.content.CourseBaseApi;
import com.xuecheng.api.content.model.dto.CourseBaseDTO;
import com.xuecheng.api.content.model.dto.CoursePubDTO;
import com.xuecheng.api.content.model.qo.QueryCourseModel;
import com.xuecheng.api.content.model.vo.CourseBaseVo;
import com.xuecheng.common.domain.page.PageRequestParams;
import com.xuecheng.common.domain.page.PageVO;
import com.xuecheng.common.domain.response.RestResponse;
import com.xuecheng.content.common.util.UAASecurityUtil;
import com.xuecheng.content.convert.CourseBaseConvert;
import com.xuecheng.content.service.CourseBaseService;
import com.xuecheng.content.service.CoursePubService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 课程基本信息 前端控制器
 * </p>
 *
 * @author itcast
 */
@Slf4j
@RestController
public class CourseBaseController implements CourseBaseApi {


    @Autowired
    private CourseBaseService courseBaseService;

    @Autowired
    private CoursePubService coursePubService;

    @PostMapping("course/list")
    public PageVO queryCourseList(PageRequestParams pageRequestParams,
                                  @RequestBody QueryCourseModel model) {

        Long companyId = UAASecurityUtil.getCompanyId();

        return courseBaseService.queryCourseList(pageRequestParams, model, companyId);
    }

    @PostMapping("course")
    public CourseBaseDTO createCourseBase(@RequestBody CourseBaseVo courseBaseVo) {

        //传入封装对象VO和返回参数封装对象DTO在Controller层处理
        CourseBaseDTO courseBaseDTO = CourseBaseConvert.INSTANCE.courseBaseVoToDto(courseBaseVo);

        Long companyId = UAASecurityUtil.getCompanyId();

        courseBaseDTO.setCompanyId(companyId);

        return courseBaseService.createCourseBase(courseBaseDTO);
    }

    @GetMapping("course/{courseBaseId}")
    public CourseBaseDTO getCourseBaseById(@PathVariable Long courseBaseId) {
        Long companyId = UAASecurityUtil.getCompanyId();

        return courseBaseService.getCourseBaseById(courseBaseId, companyId);
    }

    @PutMapping("course")
    public CourseBaseDTO modifyCourseBase(@RequestBody CourseBaseVo courseBaseVo) {
        CourseBaseDTO courseBaseDTO = CourseBaseConvert.INSTANCE.courseBaseVoToDto(courseBaseVo);

        courseBaseDTO.setCompanyId(UAASecurityUtil.getCompanyId());
        return courseBaseService.modifyCourseBase(courseBaseDTO);
    }

    @DeleteMapping("course/{courseBaseId}")
    public boolean deleteCourseBase(@PathVariable Long courseBaseId) {

        Long companyId = UAASecurityUtil.getCompanyId();

        return courseBaseService.deleteCourseBaseById(courseBaseId, companyId);
    }

    @GetMapping("course/commit/{courseBaseId}")
    public void commitCourseBase(@PathVariable Long courseBaseId) {

        Long companyId = UAASecurityUtil.getCompanyId();

        courseBaseService.commitCourseBase(courseBaseId, companyId);
    }

    /**
     * 多种视图解析器并存，返回ModelAndView 使用freemark解析器 解析返回html页面
     *
     * @param courseBaseId 课程id
     * @return ModelAndView
     */
    @GetMapping("course/preview/{courseBaseId}/{companyId}")
    public ModelAndView previewCourse(@PathVariable Long courseBaseId,
                                      @PathVariable Long companyId) {

        Map<String, Object> dataMap = coursePubService.preview(courseBaseId, companyId);

        return new ModelAndView("learing_article", dataMap);

    }
    /**
     * 运营平台 审核课程 预览
     *
     * @param courseBaseId
     * @return
     */
    @GetMapping("course/preview/{courseBaseId}")
    public ModelAndView auditPreview(@PathVariable Long courseBaseId) {

        Map<String, Object> dataMap = coursePubService.auditPreview(courseBaseId);

        return new ModelAndView("learing_article", dataMap);
    }


    @PostMapping("course_pub/publish/{courseBaseId}")
    public void publish(@PathVariable Long courseBaseId) {
        Long companyId = UAASecurityUtil.getCompanyId();
        coursePubService.publish(courseBaseId, companyId);
    }
}
